﻿package com.athena.process
{
	import com.athena.controller.AuctionController;
	import com.greensock.motionPaths.MotionPath;
	import flash.display.*;
	import flash.utils.*;
	import flash.text.TextFormat;
	import flash.events.*;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import com.adobe.serialization.json.JSON;
	import flash.net.URLRequest ;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat ;
	import flash.net.sendToURL;
	import flash.net.navigateToURL;
	/**
	 * ...
	 * @author ...
	 */
	public class AuctionFunction 
	{
		public static var TabSelect : int = 1 ;		
		
		public static var scrHistory : ScrollPane = new ScrollPane(); 
		public static var scrollHistory : MovieClip;
		
		public static var scrWinHistory : ScrollPane = new ScrollPane(); 
		public static var scrollWinHistory : MovieClip;		
		
		public static var arrKetqua : Array = null ;
		public static var arrLichsuThang : Array = null ;
		public static var arrLichsuDaugia : Array = null;
		public static var AGAuction : int = 0 ;
		public static var urlListSanpham : String = "http://services.athena.vn/ServiceGame/Sanpham.aspx?u=" ;
		public static var urlSanpham : String = "http://services.athena.vn/ServiceGame/Sanpham.aspx?aid=" ;
		public static var urlKetqua : String = "http://services.athena.vn/ServiceGame/Ketquadaugia.aspx" ;
		public static var urlTop15 : String = "http://services.athena.vn/ServiceGame/Ketquadaugia.aspx?aid=" ;
		public static var urlLichsuDaugia : String = "http://services.athena.vn/ServiceGame/Lichsudaugia.aspx?aid=" ;
		//public static var urlListSanpham : String = "http://192.168.0.101/ServiceGame/Sanpham.aspx?u=" ;
		//public static var urlSanpham : String = "http://192.168.0.101/ServiceGame/Sanpham.aspx?aid=" ;
		//public static var urlKetqua : String = "http://192.168.0.101/ServiceGame/Ketquadaugia.aspx" ;
		//public static var urlTop15 : String = "http://192.168.0.101/ServiceGame/Ketquadaugia.aspx?aid=" ;
		//public static var urlLichsuDaugia : String = "http://192.168.0.101/ServiceGame/Lichsudaugia.aspx?aid=" ;
		public static var arrAuction : Array ;
		public static var isLoad : Boolean = false ;
		
		public static var objAuction : Object ;
		public static var objAuction1 : Object ;
		public static var objLastWin : Object ;
		public static var TimerCount:Timer = new Timer(1000);
		public static var TimerAuction:Timer = new Timer(1000);
		public static var TimerAuction1:Timer = new Timer(1000);
		public static var IsCapnhat : Boolean = true ;
		
		public static var objSend : Object = new Object() ;
		
		public static function StartTimerCount(evt:TimerEvent) : void { //sau 5s duoc bam cap nhat 1 lan
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			popup.Daugia_mc.TimeRefresh_txt.text = (5 - TimerCount.currentCount).toString() ;
			popup.Daugia_mc.TimeRefresh_txt.setTextFormat(formatText) ;
			if (TimerCount.currentCount == TimerCount.repeatCount) {
				IsCapnhat = true ;
				TimerCount.stop() ;
				TimerCount = new Timer(1000) ;
				TimerCount.repeatCount = 5;
				TimerCount.addEventListener(TimerEvent.TIMER, StartTimerCount);			
				TimerCount.start() ;
				popup.Daugia_mc.TimeRefresh_txt.visible = false ;
				popup.Daugia_mc.Refresh_btn.visible = true ;
			}
		}
		public static function StartTimerAuction1(evt:TimerEvent) : void { //sau 2s hien thi nut thoat ban
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;				
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			objAuction1.Second = objAuction1.Second - 1 ;
			if (objAuction1.Second == -1) {
				objAuction1.Second = 59 ;
				objAuction1.Minute = objAuction1.Minute - 1 ;
				if (objAuction1.Minute == -1) {
					objAuction1.Minute = 59 ; 
					objAuction1.Hour = objAuction1.Hour - 1 ;
					if (objAuction1.Hour == -1) {
						objAuction1.Hour = 23 ;
						objAuction1.Day = objAuction1.Day - 1 ;
						if (objAuction1.Day == -1) {
							objAuction1.Day = 0 ;
							objAuction1.Hour = 0 ;
							objAuction1.Minute = 0 ;
							objAuction1.Second = 0 ;
						}
					} 
				}				
			}
			popup.Sanpham_mc.Day_txt.text = objAuction1.Day ; 
			popup.Sanpham_mc.Hour_txt.text = objAuction1.Hour ; 
			popup.Sanpham_mc.Minute_txt.text = objAuction1.Minute ; 
			popup.Sanpham_mc.Second_txt.text = objAuction1.Second ;
			popup.Sanpham_mc.Day_txt.setTextFormat(formatText) ;
			popup.Sanpham_mc.Hour_txt.setTextFormat(formatText) ;
			popup.Sanpham_mc.Minute_txt.setTextFormat(formatText) ;
			popup.Sanpham_mc.Second_txt.setTextFormat(formatText) ;
			if (TimerAuction1.currentCount == TimerAuction1.repeatCount) {
				TimerAuction1.stop() ;				
			}
		}
		
		public static function StartTimerAuction(evt:TimerEvent) : void { //sau 2s hien thi nut thoat ban
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			objAuction.Second = objAuction.Second - 1 ;
			if (objAuction.Second == -1) {
				objAuction.Second = 59 ;
				objAuction.Minute = objAuction.Minute - 1 ;
				if (objAuction.Minute == -1) {
					objAuction.Minute = 59 ; 
					objAuction.Hour = objAuction.Hour - 1 ;
					if (objAuction.Hour == -1) {
						objAuction.Hour = 23 ;
						objAuction.Day = objAuction.Day - 1 ;
						if (objAuction.Day == -1) {
							objAuction.Day = 0 ;
							objAuction.Hour = 0 ;
							objAuction.Minute = 0 ;
							objAuction.Second = 0 ;
						}
					} 
				}				
			}
			popup.Daugia_mc.Day_txt.text = objAuction.Day ; 
			popup.Daugia_mc.Hour_txt.text = objAuction.Hour ; 
			popup.Daugia_mc.Minute_txt.text = objAuction.Minute ; 
			popup.Daugia_mc.Second_txt.text = objAuction.Second ;
			popup.Daugia_mc.Day_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Hour_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Minute_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Second_txt.setTextFormat(formatText) ;
			if (TimerAuction.currentCount == TimerAuction.repeatCount) {
				TimerAuction.stop() ;				
			}
		}
		
		public static function LoadList_Sanpham() : void {
			//Cocaro.tf.appendText("=>ListSanpham:" + urlListSanpham) ;
			var request:URLRequest = new URLRequest(urlListSanpham + Cocaro.currPlayer.PlayerName);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			function onComplete(event:Event):void {
				arrAuction = new Array() ;
				var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
				if (popup == null) return ;
				//Cocaro.tf.appendText("=>Data:" + event.target.data) ;
				var arr : Array = (JSON.decode(event.target.data) as Array) ;
				////Cocaro.tf.appendText("=>Length:" + arr.length) ;
				for (var i:int = 0; i < arr.length; i++) 
				{
					if (i == 4) break ;
					if (i == 0) {
						objAuction1 = new Object() ;
						objAuction1.Id = arr[i].Id ;
						objAuction1.N = arr[i].N ; //Ten san pham
						objAuction1.D = arr[i].D ; //Mo ta san pham
						objAuction1.AT = arr[i].AT ; //Mo ta san pham
						objAuction1.I = arr[i].I ; //Duong dan anh
						objAuction1.PS = arr[i].PS ; //Buoc gia
						objAuction1.P = arr[i].P ; //Buoc gia
						objAuction1.WN = arr[i].WN ; //Buoc gia
						objAuction1.TV = arr[i].V; //Điều kiện về Vip
						objAuction1.ST = arr[i].ST ; //Khoang thoi gian dau gia
						objAuction1.NT = arr[i].NT ; //Khoang thoi gian dau gia
						objAuction1.FT = arr[i].FT ;
						popup.Sanpham_mc.Name_txt.text = objAuction1.N ;
						popup.Sanpham_mc.Condition_txt.text = "VIP " + objAuction1.TV + " trở lên" ;
						popup.Sanpham_mc.Price_txt.text = BasicFunction.numberFormat(objAuction1.P, 0, false, true) + " đ" ;
						//popup.Sanpham_mc.FeeAG_txt.text = "Tùy thuộc giá bạn đấu" ;
						if (objAuction1.AT == 1)
							popup.Sanpham_mc.FeeAG_txt.text = " LQ";//objAuction1.PS + " LQ" ;
						else 
							popup.Sanpham_mc.FeeAG_txt.text = " AG";//objAuction1.PS + " AG" ;
						popup.Sanpham_mc.Name_txt.setTextFormat(formatText) ;
						popup.Sanpham_mc.Condition_txt.setTextFormat(formatText) ;
						popup.Sanpham_mc.Price_txt.setTextFormat(formatText) ;
						if (objAuction1.AT == 1)
							formatText.color = 0x0000A0 ;
						popup.Sanpham_mc.FeeAG_txt.setTextFormat(formatText) ;
						var imageLoader:Loader = new Loader();
						var image:URLRequest = new URLRequest(objAuction1.I) ;
						imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
						imageLoader.name = "IAuction" ;
						imageLoader.load(image);				
						function onLoader(evt:Event)
						{
							imageLoader.width = 190 ;
							imageLoader.height = 190 ;
							popup.Sanpham_mc.Image_mc.addChild(imageLoader);
						}
						var startTime : Date = new Date(arr[i].ST) ;
						var nowTime : Date = new Date(arr[i].NT) ; 
						var endTime : Date = new Date(arr[i].FT) ;
						if (nowTime.valueOf() > startTime.valueOf()) {
							var aTms : Number = Math.floor(endTime.valueOf() - nowTime.valueOf());
							objAuction1.Day = int(aTms / (24 * 60 * +60 * 1000)) ;
							objAuction1.Hour = int(aTms / (    60 * 60 * 1000)) % 24  ;
							objAuction1.Minute = int(aTms / (       60 * 1000)) % 60  ;
							objAuction1.Second = int(aTms / (        1 * 1000)) % 60  ;
							popup.Sanpham_mc.TimeLabel_txt.text = "Thời gian còn lại" ;
							popup.Sanpham_mc.Status_txt.text = "Đang đấu giá" ;
							popup.Sanpham_mc.WinName_txt.text = objAuction1.WN;
							popup.Sanpham_mc.Status_txt.setTextFormat(formatText) ;
							popup.Sanpham_mc.WinName_txt.setTextFormat(formatText) ;
							popup.Sanpham_mc.Time_txt.visible = false ;							
							var timeCount : Number = objAuction1.Second  + objAuction1.Minute * 60 + objAuction1.Hour * 60 * 60 + objAuction1.Day * 24 * 60 * 60 - 1;
							TimerAuction1.stop() ;
							TimerAuction1 = new Timer(1000) ;
							TimerAuction1.repeatCount = timeCount;
							TimerAuction1.addEventListener(TimerEvent.TIMER, StartTimerAuction1);			
							TimerAuction1.start() ;							
						} else {
							////Cocaro.tf.appendText("=>Chua dau") ;
							popup.Sanpham_mc.TimeLabel_txt.text = "Thời gian bắt đầu" ;
							popup.Sanpham_mc.Status_txt.text = "Chuẩn bị đấu giá" ;
							popup.Sanpham_mc.WinName_txt.text = "" ;							
							////Cocaro.tf.appendText("=>1:" + arr[i].ST) ;
							popup.Sanpham_mc.Time_txt.text = startTime.getDate() + "/" + (startTime.getMonth() + 1).toString() + "/" + startTime.getFullYear() + " " + startTime.getHours() + ":" ; //
							if (startTime.getMinutes() < 10) 
								popup.Sanpham_mc.Time_txt.text = popup.Sanpham_mc.Time_txt.text + "0" + startTime.getMinutes() ;
							else
								popup.Sanpham_mc.Time_txt.text = popup.Sanpham_mc.Time_txt.text + startTime.getMinutes() ;
							////Cocaro.tf.appendText("=>3") ;
							popup.Sanpham_mc.Day_txt.visible = false ;
							popup.Sanpham_mc.Hour_txt.visible = false ;
							popup.Sanpham_mc.Minute_txt.visible = false ;
							popup.Sanpham_mc.Second_txt.visible = false ;
							popup.Sanpham_mc.DayLabel_txt.visible = false ;
							popup.Sanpham_mc.HourLabel_txt.visible = false ;
							popup.Sanpham_mc.MinuteLabel_txt.visible = false ;
							popup.Sanpham_mc.SecondLabel_txt.visible = false ;
							popup.Sanpham_mc.Status_txt.setTextFormat(formatText) ;
							popup.Sanpham_mc.Time_txt.setTextFormat(formatText) ;
							////Cocaro.tf.appendText("=>2") ;
						}
						continue ;
					}
					var obj : Object = new Object() ;
					obj.Id = arr[i].Id ;
					obj.N = arr[i].N ;
					obj.I = arr[i].I ;
					obj.PS = arr[i].PS ;
					obj.AT = arr[i].AT ;
					obj.P = arr[i].P ;
					obj.Vip = arr[i].V ;
					obj.ST = arr[i].ST ;
					obj.FT = arr[i].FT ;
					//var mc : MovieClip = MovieClip(new Auction_ItemShow()) ;
					var mc : MovieClip = MovieClip(new Cocaro.Auction_ItemShow()) ;
					new AuctionController(mc, obj) ;
					mc.name = "AuctionItem" ;
					mc.x = 16 + 245 * (i-1) ;						
					mc.y = 322 ;
					popup.Sanpham_mc.addChild(mc) ;
				}				
			}
		}
		
		public static function LoadSanpham(id : int) : void {
			////Cocaro.tf.appendText("=>Load Detail:" + id) ;
			var request:URLRequest = new URLRequest(urlSanpham + id + "&u=" + Cocaro.currPlayer.PlayerName);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				DisplaySanpham(event.target.data) ;
			}
		}
		public static function LoadTop15(id:String) : void {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			var request:URLRequest = new URLRequest(urlTop15 + id);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				////Cocaro.tf.appendText("=>Top15:" + event.target.data) ;
				var arr : Array = (JSON.decode(event.target.data) as Array) ;
				////Cocaro.tf.appendText("=>Length:" + arr.length) ;
				for (var i:int = 0; i < 15; i++) 
					popup.Daugia_mc["NameWin" + i.toString() + "_txt"].text = "" ;
				if (arr != null) {
					for (var i:int = 0; i < arr.length; i++) 
					{					
						if (i == 0) {
							popup.Sanpham_mc.WinName_txt.text = arr[i] ;
							popup.Sanpham_mc.WinName_txt.setTextFormat(formatText) ;
						}
						if (i > 14) break ;
						popup.Daugia_mc["NameWin" + i.toString() + "_txt"].text = arr[i] ;
						if (arr[i] == Cocaro.currPlayer.PlayerName)
							popup.Daugia_mc["NameWin" + i.toString() + "_txt"].setTextFormat(formatText) ;
					}
				}
			}
		}
		public static function DisplaySanpham(str : String) {
			////Cocaro.tf.appendText("=>Sanpham:" + str) ;
			isLoad = true ;
			var obj : Object = JSON.decode(str) ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			objAuction = new Object() ;
			objAuction.Id = obj.Id ;
			objAuction.N = obj.N ; //Ten san pham
			objAuction.D = obj.D ; //Mo ta san pham
			objAuction.I = obj.I ; //Duong dan anh
			objAuction.P = obj.P ; //Giá thị trường
			objAuction.AT = obj.AT ; //Buoc gia
			objAuction.PS = obj.PS ; //Buoc gia
			objAuction.TV = obj.TV; //Điều kiện về Vip
			objAuction.CA = obj.CA ; //So lan da dau
			objAuction.ST = obj.ST ; //Khoang thoi gian dau gia
			objAuction.NT = obj.NT ; //Khoang thoi gian dau gia
			objAuction.FT = obj.FT ; //Khoang thoi gian dau gia
			objAuction.Total = obj.Total ; //Số lượng đấu giá tính đến hiện tại
			popup.Daugia_mc.Status_txt.text = "Đang đấu giá" ;
			popup.Daugia_mc.Title_txt.text = obj.N ;
			popup.Daugia_mc.Description_txt.text = obj.D ;
			popup.Daugia_mc.Price_txt.text = BasicFunction.numberFormat(obj.P, 0, false, true) + " đ";
			popup.Daugia_mc.Count_txt.text = BasicFunction.numberFormat(obj.Total, 0, false, true) ;
			if (objAuction.AT == 1)
				popup.Daugia_mc.FeeAG_txt.text = BasicFunction.numberFormat(obj.PS - Cocaro.currPlayer.VIP * 50, 0, false, true) + " LQ";
			else
				popup.Daugia_mc.FeeAG_txt.text = BasicFunction.numberFormat(obj.PS - Cocaro.currPlayer.VIP * 50, 0, false, true) + " AG";
			popup.Daugia_mc.Condition_txt.text = "VIP " + obj.TV + " trở lên" ;
			if (Cocaro.currPlayer.VIP - objAuction.CA > 0)
				popup.Daugia_mc.NoteFee_txt.text = "(Miễn phí " + (Cocaro.currPlayer.VIP - objAuction.CA).toString()  + " lần đấu)" ;
			else
				popup.Daugia_mc.NoteFee_txt.text = "" ;
			popup.Daugia_mc.PriceInput_txt.text = "" ;
			popup.Daugia_mc.Title_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Condition_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Status_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Price_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Count_txt.setTextFormat(formatText) ;
			if (objAuction1.AT == 1)
				formatText.color = 0x0000A0 ;
			popup.Daugia_mc.FeeAG_txt.setTextFormat(formatText) ;
			////Cocaro.tf.appendText("=>Sanpham:7") ;
			//Tinh thoi gian
			var startTime : Date = new Date(obj.ST) ;
			var nowTime : Date = new Date(obj.NT) ; // new Date(obj.ST) ;
			var endTime : Date = new Date(obj.FT) ;
			
			var aTms : Number = Math.floor(endTime.valueOf() - nowTime.valueOf());
			objAuction.Day = int(aTms / (24 * 60 * +60 * 1000)) ;
			objAuction.Hour = int(aTms / (    60 * 60 * 1000)) % 24  ;
			objAuction.Minute = int(aTms / (       60 * 1000)) % 60  ;
			objAuction.Second = int(aTms / (        1 * 1000)) % 60  ;
			var timeCount : Number = objAuction.Second  + objAuction.Minute * 60 + objAuction.Hour * 60 * 60 + objAuction.Day * 24 * 60 * 60 - 1;
			popup.Daugia_mc.Day_txt.text = objAuction.Day ; 
			popup.Daugia_mc.Hour_txt.text = objAuction.Hour ; 
			popup.Daugia_mc.Minute_txt.text = objAuction.Minute ; 
			popup.Daugia_mc.Second_txt.text = objAuction.Second ; 
			popup.Daugia_mc.Day_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Hour_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Minute_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Second_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Day_txt.visible = true ;
			popup.Daugia_mc.Hour_txt.visible = true ;
			popup.Daugia_mc.Minute_txt.visible = true ;
			popup.Daugia_mc.Second_txt.visible = true ;
			if (TimerAuction != null)
				if (TimerAuction.running)
					TimerAuction.stop() ;
			TimerAuction = new Timer(1000) ;
			TimerAuction.repeatCount = timeCount;
			TimerAuction.addEventListener(TimerEvent.TIMER, StartTimerAuction);			
			TimerAuction.start() ;
			////Cocaro.tf.appendText("=>Sanpham:3") ;
			var imageLoader:Loader = new Loader();
			var image:URLRequest = new URLRequest(objAuction.I) ;
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
			imageLoader.name = "IAuction" ;
			imageLoader.load(image);				
			function onLoader(evt:Event)
			{
				imageLoader.width = 190 ;
				imageLoader.height = 190 ;
				popup.Daugia_mc.Image_mc.addChild(imageLoader);
			}
			LoadTop15(objAuction.Id) ;
			popup.Sanpham_btn.gotoAndStop(1) ;
			popup.Daugia_btn.gotoAndStop(2) ;
			popup.Lichsu_btn.gotoAndStop(1) ;
			popup.Luatchoi_btn.gotoAndStop(1) ;
			popup.Sanpham_mc.visible = false ;
			popup.Daugia_mc.visible = true ;
			popup.Lichsu_mc.visible = false ;
			popup.Luatchoi_mc.visible = false ;			
		}
		
		public static function ThamgiaDaugia_Click(evt : MouseEvent) {
			if (objAuction1 == null) return ;
			var startTime : Date = new Date(objAuction1.ST) ;
			var nowTime : Date = new Date(objAuction1.NT) ; 
			var endTime : Date = new Date(objAuction1.FT) ;
			if (nowTime.valueOf() > startTime.valueOf()) {
				LoadSanpham(objAuction1.Id) ;
			} else {
				BasicFunction.ShowAlert("Phiên đấu giá chưa bắt đầu, Bạn hãy quay lại sau!") ;
			}			
		}
		
		public static function Auction_Click(evt : MouseEvent) {
			////Cocaro.tf.appendText("=>Auction:") ;
			//var popup : MovieClip = MovieClip(new Popup_Daugia()) ;
			var popup : MovieClip = MovieClip(new Cocaro.Popup_Daugia) ;
			popup.Sanpham_btn.addEventListener(MouseEvent.CLICK, Sanpham_Click) ;
			popup.Daugia_btn.addEventListener(MouseEvent.CLICK, Daugia_Click) ;
			popup.Lichsu_btn.addEventListener(MouseEvent.CLICK, Lichsu_Click) ;
			popup.Luatchoi_btn.addEventListener(MouseEvent.CLICK, Luatchoi_Click) ;			
			popup.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click) ;
			popup.Sanpham_btn.gotoAndStop(2) ;
			popup.Daugia_btn.gotoAndStop(1) ;
			popup.Lichsu_btn.gotoAndStop(1) ;
			popup.Luatchoi_btn.gotoAndStop(1) ;
			popup.Sanpham_mc.visible = true ;
			popup.Daugia_mc.visible = false ;
			popup.Lichsu_mc.visible = false ;
			popup.Luatchoi_mc.visible = false ;
			popup.Daugia_mc.PriceInput_txt.text = "" ;
			popup.Daugia_mc.TimeRefresh_txt.visible = false ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			popup.Daugia_mc.PriceInput_txt.setStyle("textFormat", tfStyle);
			popup.Daugia_mc.Xacnhan_btn.addEventListener(MouseEvent.CLICK, XacnhanPrice_Click) ;
			isLoad = false ;
			
			popup.Sanpham_mc.Thamgia_btn.addEventListener(MouseEvent.CLICK, ThamgiaDaugia_Click) ;			
			popup.Daugia_mc.Refresh_btn.addEventListener(MouseEvent.CLICK, Refresh_Click) ;			
			
			popup.name = "Auction" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
			LoadList_Sanpham() ;
			Ketqua_Send() ;			
		}
		public static function Close_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			if (TimerAuction != null)
				if (TimerAuction.running)
					TimerAuction.stop() ;
			if (TimerCount != null)
				if (TimerCount.running)
					TimerCount.stop() ;
			if (TimerAuction1 != null)
				if (TimerAuction1.running)
					TimerAuction1.stop() ;
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ";	
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";	
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		public static function Refresh_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			if (!IsCapnhat) return ;
			IsCapnhat = false ;
			TimerCount = new Timer(1000) ;
			TimerCount.repeatCount = 5;
			TimerCount.addEventListener(TimerEvent.TIMER, StartTimerCount);			
			TimerCount.start() ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			popup.Daugia_mc.TimeRefresh_txt.visible = true ;
			popup.Daugia_mc.TimeRefresh_txt.text = 5 ;
			popup.Daugia_mc.TimeRefresh_txt.setTextFormat(formatText) ;
			popup.Daugia_mc.Refresh_btn.visible = false ;
			if (objAuction != null)
				LoadTop15(objAuction.Id) ;
		}
		public static function Sanpham_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			popup.Sanpham_btn.gotoAndStop(2) ;
			popup.Daugia_btn.gotoAndStop(1) ;
			popup.Lichsu_btn.gotoAndStop(1) ;
			popup.Luatchoi_btn.gotoAndStop(1) ;
			popup.Sanpham_mc.visible = true ;
			popup.Daugia_mc.visible = false ;
			popup.Lichsu_mc.visible = false ;
			popup.Luatchoi_mc.visible = false ;
		}
		public static function Daugia_Click(evt : MouseEvent) {			
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			popup.Sanpham_btn.gotoAndStop(1) ;
			popup.Daugia_btn.gotoAndStop(2) ;
			popup.Lichsu_btn.gotoAndStop(1) ;
			popup.Luatchoi_btn.gotoAndStop(1) ;
			popup.Sanpham_mc.visible = false ;
			popup.Daugia_mc.visible = true ;
			popup.Lichsu_mc.visible = false ;
			popup.Luatchoi_mc.visible = false ;
			if (!isLoad) {
				if (objAuction1 != null) {
					var startTime : Date = new Date(objAuction1.ST) ;
					var nowTime : Date = new Date(objAuction1.NT) ;
					var endTime : Date = new Date(objAuction1.FT) ;
					if (nowTime.valueOf() > startTime.valueOf()) {
						LoadSanpham(objAuction1.Id) ;
					}
				}
			}
		}
		
		public static function Lichsu_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			popup.Sanpham_btn.gotoAndStop(1) ;
			popup.Daugia_btn.gotoAndStop(1) ;
			popup.Lichsu_btn.gotoAndStop(2) ;
			popup.Luatchoi_btn.gotoAndStop(1) ;
			popup.Sanpham_mc.visible = false ;
			popup.Daugia_mc.visible = false ;
			popup.Lichsu_mc.visible = true ;
			popup.Luatchoi_mc.visible = false ;
			if (objAuction != null)
				Lichsu_Send(objAuction.Id) ;
		}
		public static function Luatchoi_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			popup.Sanpham_btn.gotoAndStop(1) ;
			popup.Daugia_btn.gotoAndStop(1) ;
			popup.Lichsu_btn.gotoAndStop(1) ;
			popup.Luatchoi_btn.gotoAndStop(2) ;
			popup.Sanpham_mc.visible = false ;
			popup.Daugia_mc.visible = false ;
			popup.Lichsu_mc.visible = false ;
			popup.Luatchoi_mc.visible = true ;
		}
		
		public static function AuctionOK(obj : Object) {
			//Cocaro.tf.appendText("=>OK:") ;
			//Cocaro.tf.appendText("=>Count:" + obj.P) ;
			BasicFunction.RemoveHideLoading() ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			//if (obj.LQ > 0)
			//	Cocaro.currPlayer.MarkLQ = obj.AG ;
			Cocaro.currPlayer.MarkLQ = obj.LQ ;
			Cocaro.currPlayer.Mark = obj.AG ;
			objAuction.CA = objAuction.CA + 1 ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			popup.Daugia_mc.Count_txt.text = obj.T ;
			popup.Daugia_mc.Count_txt.setTextFormat(formatText) ;
			if (Cocaro.currPlayer.VIP - objAuction.CA > 0)
				popup.Daugia_mc.NoteFee_txt.text = "(Miễn phí " + (Cocaro.currPlayer.VIP - objAuction.CA).toString()  + " lần đấu)" ;
			else 
				popup.Daugia_mc.NoteFee_txt.text = "" ;
			if (objAuction.AT == 1)
				popup.Daugia_mc.FeeAG_txt.text = BasicFunction.numberFormat(objAuction.PS - Cocaro.currPlayer.VIP * 50, 0, false, true) + " LQ";
			else
				popup.Daugia_mc.FeeAG_txt.text = BasicFunction.numberFormat(objAuction.PS - Cocaro.currPlayer.VIP * 50, 0, false, true) + " AG";
			//if (objAuction.AT == 1)
				//popup.Daugia_mc.FeeAG_txt.text = " LQ" ; //BasicFunction.numberFormat(obj.P, 0, false, true) +  " LQ";
			//else 
				//popup.Daugia_mc.FeeAG_txt.text = " AG" ; //BasicFunction.numberFormat(obj.P, 0, false, true) +  " AG";
			popup.Daugia_mc.FeeAG_txt.setTextFormat(formatText) ;
			////Cocaro.tf.appendText("=>AG:" + obj.AG) ;
			BasicFunction.ShowAlert("Bạn đã đấu giá thành công với mức giá " + BasicFunction.numberFormat(parseInt(popup.Daugia_mc.PriceInput_txt.text), 0, false, true) + ", Chúc bạn may mắn!")  ;
			popup.Daugia_mc.PriceInput_txt.text = "" ;
			LoadTop15(objAuction.Id) ;
		}
		
		public static function XacnhanPrice_Click(evt : MouseEvent) {
			AGAuction = 0 ; 
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			//Cocaro.tf.appendText("=>Start Price") ;
			//Cocaro.tf.appendText("=>TV:" + objAuction.TV) ;
			if (Cocaro.currPlayer.VIP < objAuction.TV) {
				BasicFunction.ShowAlert("Bạn chưa đủ cấp VIP để tham gia!") ;
				return ;
			}
			if (isNaN(parseInt(popup.Daugia_mc.PriceInput_txt.text))) {
				BasicFunction.ShowAlert("Giá bạn nhập không hợp lệ!") ;
				return ;
			}
			if (popup.Daugia_mc.PriceInput_txt.text == "") {
				BasicFunction.ShowAlert("Mời bạn nhập giá!") ;
				return ;
			}
			if (parseInt(popup.Daugia_mc.PriceInput_txt.text) <= 0) {
				BasicFunction.ShowAlert("Mức giá bạn nhập phải lớn hơn 0") ;
				return ;
			}
			//Cocaro.tf.appendText("=>1") ;			
			objSend = new Object() ;
			objSend.evt = "dg" ;
			objSend.ID = objAuction.Id ;
			objSend.AG = parseInt(popup.Daugia_mc.PriceInput_txt.text) ;
			var feeAuction : Number = 0 ;
			if (Cocaro.currPlayer.VIP - objAuction.CA <= 0) {
				feeAuction = objSend.AG * (objAuction.PS - Cocaro.currPlayer.VIP * 50) ;
				//Cocaro.tf.appendText("=>Phi dau gia:" + BasicFunction.numberFormat(feeAuction, 0, false, true)) ;
				if ((feeAuction > Cocaro.currPlayer.MarkLQ) && (objAuction.AT == 1)) {
					BasicFunction.ShowAlert("Bạn không đủ LQ để đấu giá!") ;
					return ;
				} else if ((feeAuction > Cocaro.currPlayer.Mark) && (objAuction.AT == 0)) {
					BasicFunction.ShowAlert("Bạn không đủ AG để đấu giá!") ;
					return ;
				}
				var popup : MovieClip = MovieClip(new Popup_Confirm()) ;
				popup.Title_txt.text = "ĐẤU GIÁ" ;
				if (objAuction.AT == 1)
					popup.Content_txt.text = "Với mức giá " + objSend.AG + " phí đấu giá của bạn là " + BasicFunction.numberFormat(feeAuction, 0, false, true) + "LQ";
				else 
					popup.Content_txt.text = "Với mức giá " + objSend.AG + " phí đấu giá của bạn là " + BasicFunction.numberFormat(feeAuction, 0, false, true) + "AG";
				popup.OK_btn.addEventListener(MouseEvent.CLICK, SendAution_OK_click) ;
				popup.NotOK_btn.addEventListener(MouseEvent.CLICK, SendAution_NotOK_click) ;
				popup.name = "Confirm" ;
				popup.x = 0 ;
				popup.y = 0 ;
				Cocaro.mainStage_mc.addChild(popup) ;
			} else {
				Cocaro.mainSocket.sendService(JSON.encode(objSend)) ;
				//Cocaro.tf.appendText("=>SenđG:" + JSON.encode(obj)) ;
				var mc : MovieClip = MovieClip(new Loading()) ;
				mc.x = 0 ;
				mc.y = 0 ;
				mc.name = "Loading" ;
				Cocaro.mainStage_mc.addChild(mc) ;
			}
		}
		public static function RemovePopupConfirm() {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm")) ;
			}
		}
		public static function SendAution_OK_click(evt : MouseEvent) {
			//Cocaro.tf.appendText("=>Send:" + JSON.encode(objSend)) ;
			Cocaro.mainSocket.sendService(JSON.encode(objSend)) ;
			RemovePopupConfirm() ;
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 0;
			tmpLD.y = 0;			
			Cocaro.mainStage_mc.addChild(tmpLD);
		}
		public static function SendAution_NotOK_click(evt : MouseEvent) {
			RemovePopupConfirm() ;
		}
		/***************************** Ket qua *************************************************/
		public static function KetquaReceive(str : String) {
			////Cocaro.tf.appendText("=>Ketqua:" + str) ;
			var arr : Array = (JSON.decode(str) as Array) ;
			////Cocaro.tf.appendText("=>Length:" + arr.length) ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			scrollWinHistory = new MovieClip() ;
			scrWinHistory.source = scrollWinHistory; 
			scrWinHistory.setSize(435, 345); 			
			scrWinHistory.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrWinHistory.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrWinHistory.move(507, 28.5);
			popup.Lichsu_mc.addChild(scrWinHistory) ;
			////Cocaro.tf.appendText("=>Length:" + arr.length) ;
			var formatText : TextFormat ;
			formatText = new TextFormat() ;				
			formatText.bold = true ;
			//Add item moi
			for (var i:int = 0; i < arr.length; i++) 
			{
				if (i == 0) {
					objLastWin = new Object() ;
					objLastWin.WN = arr[i].WN ;
					objLastWin.WP = arr[i].WP ;
					objLastWin.N = arr[i].N ;
					objLastWin.I = arr[i].I ;
					//Hien thi o Tab 1
					popup.Sanpham_mc.LastWinName_txt.text = objLastWin.WN ;
					popup.Sanpham_mc.LastName_txt.text = objLastWin.N ;
					popup.Sanpham_mc.LastPrice_txt.text = objLastWin.WP ;
					popup.Sanpham_mc.LastWinName_txt.setTextFormat(formatText) ;
					popup.Sanpham_mc.LastName_txt.setTextFormat(formatText) ;
					popup.Sanpham_mc.LastPrice_txt.setTextFormat(formatText) ;
					var imageLoader:Loader = new Loader();
					var image:URLRequest = new URLRequest(objLastWin.I) ;
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "IAuction" ;
					imageLoader.load(image);		
					function onLoader(evt:Event)
					{
						imageLoader.width = 130 ;
						imageLoader.height = 130 ;
						popup.Sanpham_mc.LastImage_mc.addChild(imageLoader);
					}
					if (objAuction == null) {
						Lichsu_Send(arr[i].Id) ;
					}
				}
				//var mc : MovieClip = MovieClip(new AuctionWin_Item()) ;
				var mc : MovieClip = MovieClip(new Cocaro.AuctionWin_Item) ;
				mc.User_txt.text = arr[i].WN ;
				mc.Name_txt.text = arr[i].N ;
				mc.Price_txt.text = BasicFunction.numberFormat(arr[i].WP, 0, false, true) ;
				mc.name = "Ketqua" ;
				mc.x = 10 ;
				mc.y = i * 21 ;
				scrollWinHistory.addChild(mc) ;
				scrWinHistory.update() ;
			}
			////Cocaro.tf.appendText("=>Ketquafinished") ;
		}
		public static function Ketqua_Send() {
			////Cocaro.tf.appendText("=>KetquaSend:" + urlKetqua) ;
			var request:URLRequest = new URLRequest(urlKetqua);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				KetquaReceive(event.target.data) ;
			}
		}
		/***************************** Lich su ************************************************/
		public static function LichsuReceive(str : String) {
			////Cocaro.tf.appendText("=>Lichsu:" + str) ;
			var obj : Object = JSON.decode(str) ;
			////Cocaro.tf.appendText("==>P:" + obj.P) ;
			////Cocaro.tf.appendText("==>AS:" + obj.AS) ;
			//var arr : Array = (JSON.decode(obj.AS) as Array) ;
			////Cocaro.tf.appendText("==>1") ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Auction")) ;
			if (popup == null) return ;
			scrollHistory = new MovieClip() ;
			scrHistory.source = scrollHistory; 
			scrHistory.setSize(290, 345); 			
			scrHistory.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrHistory.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrHistory.move(203, 28.5);
			popup.Lichsu_mc.addChild(scrHistory) ;			
			////Cocaro.tf.appendText("=>Length:" + obj.AS.length) ;
			//Add item moi
			for (var i:int = 0; i < obj.AS.length; i++) 
			{
				////Cocaro.tf.appendText("=>i:" + i + "-" + obj.AS[i].P) ;
				//var mc : MovieClip = MovieClip(new Auction_Item()) ;
				var mc : MovieClip = MovieClip(new Cocaro.Auction_Item) ;
				//mc.Name_txt.text = objAuction.N ;
				mc.Price_txt.text = BasicFunction.numberFormat(obj.AS[i].P, 0, false, true) ;
				mc.Time_txt.text = obj.AS[i].T ;
				//mc.Stt_txt.text = (i + 1).toString() ;
				mc.name = "Lichsu" ;
				mc.x = 19 ;
				mc.y = i * 21 ;
				scrollHistory.addChild(mc) ;
				scrHistory.update() ;
				//popup.Lichsu_mc.addChild(mc) ;
			}
		}
		public static function Lichsu_Send(id : int) {
			//if (objAuction == null) return ;
			//if (objAuction.Id == null) return ;
			var request:URLRequest = new URLRequest(urlLichsuDaugia + id + "&u=" + Cocaro.currPlayer.PlayerName);
			request.method = URLRequestMethod.POST;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			loader.load(request);
			function onComplete(event:Event):void {
				LichsuReceive(event.target.data) ;
			}
		}		
	}
}